<?php
/* SVN FILE: $Id: wages.php 191 2009-09-01 22:19:51Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* Manage Forums
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 191 $
* @lastrevision $Date: 2009-09-01 22:19:51 +0000 (Tue, 01 Sep 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-09-01 22:19:51 +0000 (Tue, 01 Sep 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/modules_admin/manage/wages.php $
*/


if ( ! defined( 'IN_ACP' ) )
{
	print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded 'admin.php'.";
	exit();
}

class admin_ibmarket_manage_wages extends ipsCommand
{
	/**
	 * Skin object
	 *
	 * @access	private
	 * @var		object			Skin templates
	 */
	private $html;
	
	/**
	 * Main class entry point
	 *
	 * @access	public
	 * @param	object		ipsRegistry reference
	 * @return	void		[Outputs to screen]
	 */
	public function doExecute( ipsRegistry $registry )
	{
		$this->registry->getClass('class_permissions')->checkPermissionAutoMsg( 'manage_wages' );
		
		$this->html = $this->registry->output->loadTemplate('cp_skin_manage');
		
		$this->registry->getClass('class_localization')->loadLanguageFile( array( 'admin_market', 'admin_manage' ) );
		
		$execute = array(
							'manage' => 'manageWages',
							'add' => 'addWage',
							'insert' => 'insertWage',
							'edit' => 'editWage',
							'update' => 'updateWage',
						);
		
		if(!isset($execute[$this->request['do']]))
		{
			$this->request['do'] = 'manage';
		}
		
		$function = $execute[$this->request['do']];
		$this->$function();
	}
	
	public function manageWages()
	{
		$members = array();
		foreach($this->caches['market_wages']['id'] as $wage)
		{
			if($wage['to_who'] == 'm')
			{
				$members[$wage['id_who']] = "";
			}
		}
		
		if(count($members))
		{
			$this->DB->build(array('select'=>'*','from'=>'members','where'=>"member_id IN (".implode(", ", array_keys($members)).")"));
			$this->DB->execute();
			while($member = $this->DB->fetch())
			{
				$members[$member['member_id']] = marketFormatter::formatName($member);
			}
		}
		
		$this->registry->output->html = $this->html->manageWages($members);
		
		$this->registry->getClass('output')->html_main .= $this->registry->getClass('output')->global_template->global_frame_wrapper();
		$this->registry->getClass('output')->sendOutput();
	}
	
	public function addWage()
	{
		$wage = array();
		$wage['how_much'] = 1;
		$wage['w_how'] = 2;
		$wage['w_when'] = IPS_UNIX_TIME_NOW - IPS_UNIX_TIME_NOW % 60;
		$wage['w_day'] = 0;
		$wage['w_hour'] = 0;
		$wage['w_minute'] = 0;
		$this->registry->output->html = $this->html->wageForm($wage, 'insert', $this->lang->words['add_wage_title'], $this->lang->words['add_wage_button']);
		
		$this->registry->getClass('output')->html_main .= $this->registry->getClass('output')->global_template->global_frame_wrapper();
		$this->registry->getClass('output')->sendOutput();
	}
	
	public function insertWage()
	{
		ipsRegistry::getClass('adminFunctions')->checkSecurityKey();
		
		$wage = array();
		if($this->request['member_name'] == '')
		{
			if(!is_array($this->caches['group_cache'][$this->request['group_id']]))
			{
				$this->registry->output->silentRedirectWithMessage( $this->settings['base_url']."module=manage&amp;section=wages&amp;do=add" );
			}
			$wage['to_who'] = 'g';
			$wage['id_who'] = $this->request['group_id'];
		}
		else
		{
			$member = $this->DB->buildAndFetch(array('select'=>'*','from'=>'members','where'=>"members_display_name='".$this->request['member_name']."'"));
			if(!$member)
			{
				$this->registry->output->silentRedirectWithMessage( $this->settings['base_url']."module=manage&amp;section=wages&amp;do=add" );
			}
			$wage['to_who'] = 1;
			$wage['id_who'] = $member['member_id'];
		}
		$wage['how_much'] = numbers::parseInt($this->request['how_much']);
		$wage['w_how'] = max(min($this->request['w_how'], 2), 1);
		if($this->request['w_how'] == 1)
		{
			list($date, $time) = explode(" ",$this->request['w_when']);
			list($month,$day,$year) = explode("/",$date);
			list($hour,$minute) = explode(":",$time);
			$wage['w_when'] = gmmktime( intval($hour), intval($minute), 0, intval($month), intval($day), intval($year) );
		}
		else
		{
			$wage['w_when'] = 0;
		}
		
		$wage['w_day'] = max(min(intval($this->request['w_day']), 7), 0);
		$wage['w_hour'] = max(min(intval($thisnext_payment->request['w_hour']), 24), 0);
		$wage['w_minute'] = max(min(intval($this->request['w_minute']), 60), 0);
		$wage['next_payment'] = marketRegistry::nextWageTime($wage);
		
		$this->DB->insert('market_wages', $wage);
		
		$this->cache->rebuildCache('market_wages','ibmarket');
		
		ipsRegistry::getClass('adminFunctions')->saveAdminLog( $this->lang->words['add_wage_log'] );
		
		$this->registry->output->global_message = $this->lang->words['wage_added'];
		$this->registry->output->silentRedirectWithMessage( $this->settings['base_url']."module=manage&amp;section=wages" );
	}
	
	public function editWage()
	{
		if(!is_array($this->caches['market_wages']['id'][$this->request['wage']]))
		{
			$this->registry->output->showError(null);
		}
		
		$this->registry->output->html = $this->html->wageForm($this->caches['market_wages']['id'][$this->request['wage']], 'update', $this->lang->words['edit_wage_title'], $this->lang->words['edit_wage_button']);
		
		$this->registry->getClass('output')->html_main .= $this->registry->getClass('output')->global_template->global_frame_wrapper();
		$this->registry->getClass('output')->sendOutput();
	}
	
	public function updateWage()
	{
		if(!is_array($this->caches['market_wages']['id'][$this->request['wage']]))
		{
			$this->registry->output->showError(null);
		}
		
		ipsRegistry::getClass('adminFunctions')->checkSecurityKey();
		
		$wage = array();
		$wage['how_much'] = numbers::parseInt($this->request['how_much']);
		$wage['w_how'] = max(min($this->request['w_how'], 2), 1);
		if($this->request['w_how'] == 1)
		{
			list($date, $time) = explode(" ",$this->request['w_when']);
			list($month,$day,$year) = explode("/",$date);
			list($hour,$minute) = explode(":",$time);
			$wage['w_when'] = gmmktime( intval($hour), intval($minute), 0, intval($month), intval($day), intval($year) );
		}
		else
		{
			$wage['w_when'] = 0;
		}
		
		$wage['w_day'] = max(min(intval($this->request['w_day']), 7), 0);
		$wage['w_hour'] = max(min(intval($thisnext_payment->request['w_hour']), 24), 0);
		$wage['w_minute'] = max(min(intval($this->request['w_minute']), 60), 0);
		$wage['next_payment'] = marketRegistry::nextWageTime($wage);
		
		$this->DB->update('market_wages', $wage, "w_id=".$this->request['wage']);
		
		$this->cache->rebuildCache('market_wages','ibmarket');
		
		ipsRegistry::getClass('adminFunctions')->saveAdminLog( $this->lang->words['edit_wage_log'] );
		
		$this->registry->output->global_message = $this->lang->words['wage_edited'];
		$this->registry->output->silentRedirectWithMessage( $this->settings['base_url']."module=manage&amp;section=wages" );
	}
}